/*
 * Log.cpp
 *
 *  Created on: 2013-4-27
 *      Author: song
 */

#include "SmtPlug.h"
#include <sys/syscall.h>
#define gettid() syscall(__NR_gettid)

int SmtPlug::LogUsrData(struct sockaddr_in &addr,HValue_t &value)
{
	uint16 cmd=value.head.cmd;
	switch (cmd)
	{

		case ADMIN_DEV_ADD:
		case ADMIN_DEV_DELETE:
		case ADMIN_USR_ADD:
		case ADMIN_USR_DELETE:
			LogUsrAdminData(addr,value,"UsrLog_Admin");
			break;

		case SET_DEV_POLL_FREQ:
			LogUsrSetPollFreq(addr,value,"UsrLog_SetDevPollFreq");
			break;

		case USR_MODIFY_PSWD:
			LogUsrModifyPswd(addr,value,"UsrLog_SetPswd");
			break;


		case EMIT_IRCODE:
			LogUsrDataIRCode(addr,value,"UsrP11_IRCode_Send");
			break;
		default:
			LogUsrDataDefault(addr,value,"UsrLog_Cmd");
			break;
	}

	return 0;
}

int SmtPlug::LogDevData(struct sockaddr_in &addr,HValue_t &value)
{
	uint16 cmd=value.head.cmd;
	switch (cmd)
	{
		case UP_IRCODE:
			LogDevDataIRCode(addr,value,"DevLog_IRCode_Rec");
			break;

		case CX_ALL_VALUE:
		case UP_ALL_VALUE:
		case UP_ALL_VALUE_FROM_FLASH:
			LogDevAllValue(addr,value,"DevLog_Value");
			break;


		}
	return 0;
}

int SmtPlug::LogUsrDataDefault(struct sockaddr_in &addr,HValue_t &value,char *sTable)
{
	char strSql[300];
	struct timeval tv;
	gettimeofday(&tv,NULL);
	//printf("now tv_sec=%d,tv_usec=%d\n",tv.tv_sec,tv.tv_usec);

	uint8* pp=(uint8*)&value;
	for(uint8 i=0;i<30;i++)
		printf("%02x ",pp[i]);
	printf("\n");

	printf("value.head.src.id= %d\n",value.head.src.id);
	printf("value.head.dst.id= %d\n",value.head.dst.id);


	sprintf(strSql,"insert into %s(RecDate,RecTime,RecTimeUsec,UsrId,UsrDate,UsrTime,IP,Port,DevId,Cmd) Values('%s','%s',%d,%d,'%s','%s','%s',%d,%d,%d)",
			sTable,
			DateTime::GetNowDate().c_str(),
			DateTime::GetNowTime().c_str(),
			tv.tv_usec,
			value.head.src.id,
			/*DateTime::GetNowDate().c_str(),*/DateTime::GetTmtDate((time_t)value.head.tm).c_str(),
			/*DateTime::GetNowTime().c_str(),*/DateTime::GetTmtTime((time_t)value.head.tm).c_str(),

			inet_ntoa(addr.sin_addr),
			ntohs(addr.sin_port),
			value.head.dst.id,
			value.head.cmd
			);

	cout<<strSql<<endl;


	if(!pUsrDB->exec(strSql))
	{
		cout<<"AddErrorLog failure!"<<endl;
	}
		//cout<<"success"<<endl;

	//usleep(5000*1000);

	//cout<<"success"<<endl;
}


int SmtPlug::LogUsrAdminData(struct sockaddr_in &addr,HValue_t &value,char *sTable)
{
	char strSql[300];
	struct timeval tv;
	gettimeofday(&tv,NULL);
	//printf("now tv_sec=%d,tv_usec=%d\n",tv.tv_sec,tv.tv_usec);

	uint8* pp=(uint8*)&value;
	for(uint8 i=0;i<30;i++)
		printf("%02x ",pp[i]);
	printf("\n");

	printf("value.head.src.id= %d\n",value.head.src.id);
	printf("value.head.dst.id= %d\n",value.head.dst.id);


	AdminId_t *pAdminId=(AdminId_t*)value.value.szValue;

	sprintf(strSql,"insert into %s(RecDate,RecTime,RecTimeUsec,UsrId,UsrDate,UsrTime,IP,Port,DevIdFrom,DevIdTo,Cmd) Values('%s','%s',%d,%d,'%s','%s','%s',%d,%d,%d,%d)",
			sTable,
			DateTime::GetNowDate().c_str(),
			DateTime::GetNowTime().c_str(),
			tv.tv_usec,
			value.head.src.id,
			/*DateTime::GetNowDate().c_str(),*/DateTime::GetTmtDate((time_t)value.head.tm).c_str(),
			/*DateTime::GetNowTime().c_str(),*/DateTime::GetTmtTime((time_t)value.head.tm).c_str(),

			inet_ntoa(addr.sin_addr),
			ntohs(addr.sin_port),
			pAdminId->firstId,
			pAdminId->lastId,
			value.head.cmd
			);

	cout<<strSql<<endl;


	if(!pUsrDB->exec(strSql))
	{
		cout<<"AddErrorLog failure!"<<endl;
	}
		//cout<<"success"<<endl;

	//usleep(5000*1000);

	//cout<<"success"<<endl;
}


int SmtPlug::LogUsrSetPollFreq(struct sockaddr_in &addr,HValue_t &value,char *sTable)
{

	char strSql[300];
	struct timeval tv;
	gettimeofday(&tv,NULL);
	//printf("now tv_sec=%d,tv_usec=%d\n",tv.tv_sec,tv.tv_usec);

	uint8* pp=(uint8*)&value;
	for(uint8 i=0;i<30;i++)
		printf("%02x ",pp[i]);
	printf("\n");

	printf("value.head.src.id= %d\n",value.head.src.id);
	printf("value.head.dst.id= %d\n",value.head.dst.id);


	sprintf(strSql,"insert into %s(RecDate,RecTime,RecTimeUsec,UsrId,UsrDate,UsrTime,IP,Port,DevId,Cmd,PollFreq) Values('%s','%s',%d,%d,'%s','%s','%s',%d,%d,%d,%d)",
			sTable,
			DateTime::GetNowDate().c_str(),
			DateTime::GetNowTime().c_str(),
			tv.tv_usec,
			value.head.src.id,
			/*DateTime::GetNowDate().c_str(),*/DateTime::GetTmtDate((time_t)value.head.tm).c_str(),
			/*DateTime::GetNowTime().c_str(),*/DateTime::GetTmtTime((time_t)value.head.tm).c_str(),

			inet_ntoa(addr.sin_addr),
			ntohs(addr.sin_port),
			value.head.dst.id,
			value.head.cmd,
			(uint16)value.value.u32Value
			);

	cout<<strSql<<endl;


	if(!pUsrDB->exec(strSql))
	{
		cout<<"AddErrorLog failure!"<<endl;
	}
		//cout<<"success"<<endl;

	//usleep(5000*1000);

	//cout<<"success"<<endl;

}

int SmtPlug::LogUsrModifyPswd(struct sockaddr_in &addr,HValue_t &value,char *sTable)
{

	char strSql[300];
	struct timeval tv;
	gettimeofday(&tv,NULL);
	//printf("now tv_sec=%d,tv_usec=%d\n",tv.tv_sec,tv.tv_usec);

	uint8* pp=(uint8*)&value;
	for(uint8 i=0;i<30;i++)
		printf("%02x ",pp[i]);
	printf("\n");

	printf("value.head.src.id= %d\n",value.head.src.id);
	printf("value.head.dst.id= %d\n",value.head.dst.id);


	sprintf(strSql,"insert into %s(RecDate,RecTime,RecTimeUsec,UsrId,UsrDate,UsrTime,IP,Port,DevId,Cmd,Pswd) Values('%s','%s',%d,%d,'%s','%s','%s',%d,%d,%d,'%s')",
			sTable,
			DateTime::GetNowDate().c_str(),
			DateTime::GetNowTime().c_str(),
			tv.tv_usec,
			value.head.src.id,
			/*DateTime::GetNowDate().c_str(),*/DateTime::GetTmtDate((time_t)value.head.tm).c_str(),
			/*DateTime::GetNowTime().c_str(),*/DateTime::GetTmtTime((time_t)value.head.tm).c_str(),

			inet_ntoa(addr.sin_addr),
			ntohs(addr.sin_port),
			value.head.dst.id,
			value.head.cmd,
			value.value.szValue
			);

	cout<<strSql<<endl;


	if(!pUsrDB->exec(strSql))
	{
		cout<<"AddErrorLog failure!"<<endl;
	}
		//cout<<"success"<<endl;

	//usleep(5000*1000);

	//cout<<"success"<<endl;


}


int SmtPlug::LogUsrDataIRCode(struct sockaddr_in &addr,HValue_t &value,char *sTable)
{

	struct timeval tv;
	gettimeofday(&tv,NULL);
	//printf("now tv_sec=%d,tv_usec=%d\n",tv.tv_sec,tv.tv_usec);


	IRInfo_t *pIRInfo=(IRInfo_t*)value.value.szValue;
	char strSql[4000];

	sprintf(strSql,"insert into %s"
			"(RecDate,RecTime,RecTimeUsec,UsrId,UsrDate,usrTime,IP,Port,DevId,"
			"CodeType,DataLen, "

			"Head0,Head1,Head2,Head3,Head4,Head5,Head6,Head7,"
			"Data0,Data1,Data2,Data3,Data4,Data5,Data6,Data7,Data8,Data9,Data10,"
			"Data11,Data12,Data13,Data14,Data15,Data16,Data17,Data18,Data19,"
			"Tail0,Tail1,Tail2,Tail3,Tail4,Tail5,Tail6,Tail7) "
			"Values "
			"('%s','%s',%d,%d,'%s','%s','%s',%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,"
			"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,"
			"%d,%d,%d,%d,%d,%d,%d,%d)",
		sTable,
		DateTime::GetNowDate().c_str(),
		DateTime::GetNowTime().c_str(),
		tv.tv_usec,value.head.src.id,
		/*DateTime::GetNowDate().c_str(),*/DateTime::GetTmtDate((time_t)value.head.tm).c_str(),
		/*DateTime::GetNowTime().c_str(),*/DateTime::GetTmtTime((time_t)value.head.tm).c_str(),
		inet_ntoa(addr.sin_addr),ntohs(addr.sin_port),
		value.head.dst.id,
		pIRInfo->type,pIRInfo->dataNum,

		pIRInfo->head[0],pIRInfo->head[1],pIRInfo->head[2],pIRInfo->head[3],pIRInfo->head[4],pIRInfo->head[5],pIRInfo->head[6],pIRInfo->head[7],
		pIRInfo->data[0],pIRInfo->data[1],pIRInfo->data[2],pIRInfo->data[3],pIRInfo->data[4],pIRInfo->data[5],pIRInfo->data[6],pIRInfo->data[7],pIRInfo->data[8],
		pIRInfo->data[9],pIRInfo->data[10],pIRInfo->data[11],pIRInfo->data[12],pIRInfo->data[13],pIRInfo->data[14],pIRInfo->data[15],pIRInfo->data[16],pIRInfo->data[17],
		pIRInfo->data[18],pIRInfo->data[19],
		pIRInfo->tail[0],pIRInfo->tail[1],pIRInfo->tail[2],pIRInfo->tail[3],pIRInfo->tail[4],pIRInfo->tail[5],pIRInfo->tail[6],pIRInfo->tail[7]

	);

	cout<<strSql<<endl;




	if(!pUsrDB->exec(strSql))
	{
		cout<<"AddErrorLog failure!"<<endl;
	}
		cout<<"success"<<endl;


}


int SmtPlug::LogDevDataIRCode(struct sockaddr_in &addr,HValue_t &value,char *sTable)
{

	struct timeval tv;
	gettimeofday(&tv,NULL);
	//printf("now tv_sec=%d,tv_usec=%d\n",tv.tv_sec,tv.tv_usec);


	IRInfo_t *pIRInfo=(IRInfo_t*)value.value.szValue;
	char strSql[4000];

	sprintf(strSql,"insert into %s"
		"(RecDate,RecTime,RecTimeUsec,DevId,DevDate,DevTime,IP,Port,  "
		"CodeType,DataLen, "

		"Head0,Head1,Head2,Head3,Head4,Head5,Head6,Head7,"
		"Data0,Data1,Data2,Data3,Data4,Data5,Data6,Data7,Data8,Data9,Data10,"
		"Data11,Data12,Data13,Data14,Data15,Data16,Data17,Data18,Data19,"
		"Tail0,Tail1,Tail2,Tail3,Tail4,Tail5,Tail6,Tail7) "
		"Values "
		"('%s','%s',%d,%d,'%s','%s','%s',%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,"
		"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,"
		"%d,%d,%d,%d,%d,%d,%d,%d)",
	sTable,

	DateTime::GetNowDate().c_str(),DateTime::GetNowTime().c_str(),tv.tv_usec,value.head.src.id,DateTime::GetTmtDate((time_t)value.head.tm+TIMECHAZHI).c_str(),DateTime::GetTmtTime((time_t)value.head.tm+TIMECHAZHI).c_str(),inet_ntoa(addr.sin_addr),ntohs(addr.sin_port),
	pIRInfo->type,pIRInfo->dataNum,

	pIRInfo->head[0],pIRInfo->head[1],pIRInfo->head[2],pIRInfo->head[3],pIRInfo->head[4],pIRInfo->head[5],pIRInfo->head[6],pIRInfo->head[7],
	pIRInfo->data[0],pIRInfo->data[1],pIRInfo->data[2],pIRInfo->data[3],pIRInfo->data[4],pIRInfo->data[5],pIRInfo->data[6],pIRInfo->data[7],pIRInfo->data[8],
	pIRInfo->data[9],pIRInfo->data[10],pIRInfo->data[11],pIRInfo->data[12],pIRInfo->data[13],pIRInfo->data[14],pIRInfo->data[15],pIRInfo->data[16],pIRInfo->data[17],
	pIRInfo->data[18],pIRInfo->data[19],
	pIRInfo->tail[0],pIRInfo->tail[1],pIRInfo->tail[2],pIRInfo->tail[3],pIRInfo->tail[4],pIRInfo->tail[5],pIRInfo->tail[6],pIRInfo->tail[7]

	);
	cout<<strSql<<endl;




	if(!pDevDB->exec(strSql))
	{
		cout<<"AddErrorLog failure!"<<endl;
	}
		cout<<"success"<<endl;



}


int SmtPlug::LogDevAllValue(struct sockaddr_in &addr,HValue_t &value,char* sTable)
{

	char strSql[1000];
	struct timeval tv;
	gettimeofday(&tv,NULL);
	//printf("now tv_sec=%d,tv_usec=%d\n",tv.tv_sec,tv.tv_usec);



	Data_t *pData=(Data_t*)value.value.szValue;

	//时间不对，不记
	if(value.head.tm <3600000 )
		return 0;

	uint8 doPonit[8]={0};
	for(uint8 i=0;i<8;i++)
	{
		if(pData->dopoint>>i & 0x01)
			doPonit[i]=1;
		else
			doPonit[i]=0;

	}

	sprintf(strSql,"insert into %s(RecDate,RecTime,RecTimeUsec,DevId,DevDate,DevTime,IP,Port,UsrId,Temp,Hu,Cur,Do0,Do1,Do2,Do3,Do4,Do5,Do6) Values('%s','%s',%d,%d,'%s','%s','%s',%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)",
	sTable,
	DateTime::GetNowDate().c_str(),
	DateTime::GetNowTime().c_str(),
	tv.tv_usec,
	value.head.src.id,
	DateTime::GetTmtDate((time_t)value.head.tm+TIMECHAZHI).c_str(),
	DateTime::GetTmtTime((time_t)value.head.tm+TIMECHAZHI).c_str(),
	inet_ntoa(addr.sin_addr),
	ntohs(addr.sin_port),
	value.head.dst.id,
	pData->th,
	pData->hh,
	pData->cur,
	doPonit[0],doPonit[1],doPonit[2],doPonit[3],doPonit[4],doPonit[5],doPonit[6]
	);
	//cout<<strSql<<endl;


	if(!pDevDB->exec(strSql))
	{
		cout<<"AddErrorLog failure!"<<endl;
	}
		cout<<"success"<<endl;


}

